<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    zaki<>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mc_type.class.php 2011-07-06 21:57:09 $
 * @Show Note: 
 */
Class zy_mc_type extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = PAGENO;
		
		/**
		 *	Construct this module class.
		*/
		public function zy_mc_type()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="zy_mc_type.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("zy_mc_type");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign('sell_type_arr',get_sell_type_arr());//购置性质数组
			$this->smarty->assign('project_arr',get_project_arr());//项目数组
			$this->smarty->assign('is_exist_c_mc',get_is_exist_c_mc());//是否存在子设备数组
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->zy_mc_type();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
				$col = "v_mc_id";
				$where = 1;
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						$investment_no = isset($_REQUEST['investment_no']) ? htmlspecialchars(trim($_REQUEST['investment_no'])) : '';//投资自编号
						if (empty($investment_no)) {
							alertMsg('-2055');
						}
						$v_mc_id = isset($_GET['v_mc_id']) ? intval($_GET['v_mc_id']) : 0;//虚拟设备ID
						if ($v_mc_id) {
							//使用模版，展示提交界面
							$result = $this->db->getOne($v_mc_id);
							if(empty($result))
							{
								alertMsg('-2001', 'index.php?c=zy_mc_type');
							}
							//去掉项目 申请数量 备注 
							//unset($result['plan_no']);
							unset($result['mc_info']);
							unset($result['project_id']);
							
							foreach ($result AS $k=>$v)
							{
								$this->smarty->assign("{$k}",$v);
							}
						}
						//使用模版，展示提交界面
						$this->smarty->assign('investment_no',$investment_no);//计划自编号
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_zy_mc_type($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($insrt < 0) {
							alertMsg($error_no, "index.php?c=zy_nvestment&m=browse&investment_no={$temp['investment_no']}");
						}else{
							redirectPage("index.php?c=zy_nvestment&m=browse&investment_no={$temp['investment_no']}");
						}
						//alertMsg($error_no, 'index.php?c=zy_nvestment');
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$v_mc_id = isset($_REQUEST['v_mc_id']) ? intval(trim($_REQUEST['v_mc_id'])) : 0 ;
				$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : '';
				if (empty($investment_no)) {
					alertMsg('-2055');
				}
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($v_mc_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=zy_mc_type');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('investment_no',$investment_no);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "v_mc_id=$v_mc_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_zy_mc_type($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($update < 0) {
							alertMsg($error_no, "index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
						}else{
							redirectPage("index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
						}
						//alertMsg($error_no, "index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
				}
		}
		/**
		 *	browse method.
		*/
		public function browse() {
			$col = "v_mc_id";
			$where = 1;
			$zh_name = isset($_GET['zh_name']) ? trim($_GET['zh_name']) : '';
			if (!empty($zh_name)) {
				$where .= " AND mc_cn_name like '%{$zh_name}%'";
			}
			$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
			//数据库执行sql语句出错
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if( 0 > $rowNo) goToIndex($rowNo);
			//分页
			$page = Factory::getBaseClass('pagination');
			$page->set($rowNo,$this->pageSize);
			$numrows = $this->pageSize;
			$offset  = $page->getSqlMove();
			$arr = $this->db->getAll($where,$col,$numrows,$offset);
			admin_log($this->admin_user_id,__METHOD__,'查看',1);
			//数据库执行select语句出错
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if(0 > $arr ) goToIndex($arr);
			
			$this->smarty->assign('arr',$arr);
			//默认方法
			$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
			$this->smarty->assign('act','browse');
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$v_mc_id = isset($_REQUEST['v_mc_id']) ? intval($_REQUEST['v_mc_id']) : 0 ;
				$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : '';
				//如果被批复则不能被删除(批复设备关联表中查看)
				$temp = $this->check_is_exist($v_mc_id);
				if (!empty($temp)) {
					alertMsg('-2058');
				}
				$where = "v_mc_id=$v_mc_id";
				$del=$this->db->delete_zy_mc_type($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if ($del < 0) {
					alertMsg($error_no, "index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
				}else{
					redirectPage("index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
				}
				//alertMsg($error_no, "index.php?c=zy_nvestment&m=browse&investment_no=$investment_no");
		}

		/**
	 *	import method.
	 */
	public function imports()
	{
		$opt = isset($_POST['opt']) ? intval($_POST['opt']) : 0 ;
		$id = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : '' ;
		if(empty($id)) {
			alertMsg2("操作有误", 'index.php?c=zy_nvestment');
		}
		if(empty($opt))	{
			$this->smarty->assign('act_title', '导入数据');
			$this->smarty->assign('act', 'imports');
			$this->smarty->assign('control', 'zy_mc_type');
			$this->smarty->assign('investment_no', $id);
			$this->smarty->display($this->tplFile);
		} else {
			//1. 上传xls
			$savePath = UPLOAD_PATH . 'csv/zy_mc_type/';
			if(!file_exists($savePath)) {
				mk_dir($savePath);
			}
			$uploadRes = $this->upload_file_xls($savePath, 'fileName', array('csv'));
			if(0 == $uploadRes['state']) {
				alertMsg2($uploadRes['msg'], 'index.php?c=zy_nvestment');
			}
			$fileName = $uploadRes['msg']; //上传成功后文件名

			//2. 上传成功后读取xls数据分析
			$item_pe = Factory::getItemClass('parse_excel');

			//获取xls文件数据
			$filePath = $savePath.$fileName;
			//$arr_xls = $item_pe->data_xls($filePath);
			//$out = $item_pe->data_parse_xls($arr_xls);

			$arr_csv = $item_pe->data_csv($filePath);
			$out = is_array($arr_csv['data']) ? $arr_csv['data'] : array();

			if(empty($out)) {
				alertMsg2("读取文件内容失败或数据为空", 'index.php?c=zy_nvestment');
			}

			unset($out[0]); //去掉标题栏

			//3. 入库
			$db_zmt = Factory::getDB('zy_mc_type');
			foreach($out as $k => $v) {
				if(!empty($v)) {
					$val['investment_no'] = $id; //
					//$val['admin_user_id'] = $this->admin_user_id; //录入用户：后台登录用户
					if(!empty($v[0]) && !empty($v[1])) {
						$val['project_id'] = $v[0];
						$val['mc_cn_name'] = $v[1];
						$val['mc_en_name'] = isset($v[2]) ? trim($v[2]) : '';
						$val['mc_type_model'] = isset($v[3]) ? trim($v[3]) : '';
						$val['plan_no'] = $v[4];
						$val['make_time'] = $v[5];
						$val['make_cost'] = $v[6];
						$val['manage_cost'] = $v[7];
						$val['vat'] = $v[8];
						$val['is_p_mc'] = $v[9];
						$val['mc_info'] = $v[10];
						$val['sell_type_id'] = $v[11];
						$val['unit'] = $v[12];
						
						$resId = $db_zmt->insert_zy_mc_type($val);
					}
				}
			}
			//redirectPage('index.php?c=zy_mend');
			alertMsg2('导入完成', "index.php?c=zy_nvestment&m=browse&investment_no={$id}");
		}
	}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['v_mc_id'] = $_POST['v_mc_id'];
			} else {
				$temp['v_mc_id'] = "";
				$temp['investment_no'] = isset($_POST['investment_no']) ? htmlspecialchars(trim($_POST['investment_no'])) : '';//投资自编号
				if (empty($temp['investment_no'])) {
					alertMsg('-2055');
				}
				$temp['is_p_mc'] = isset($_POST['is_p_mc']) ? intval($_POST['is_p_mc']) : 0;//是否是子设备 0为不存 1为存在
			}
			//补充其他的字段
			$temp['project_id'] = isset($_POST['project_id']) ? intval($_POST['project_id']) : 0;//项目
			if (empty($temp['project_id'])) {
				alertMsg('2057');
			}
			$temp['mc_cn_name'] = isset($_POST['mc_cn_name']) ? htmlspecialchars(trim($_POST['mc_cn_name'])) : '';//中文名字
			$temp['mc_en_name'] = isset($_POST['mc_en_name']) ? htmlspecialchars(trim($_POST['mc_en_name'])) : '';//英文名字
			$temp['mc_type_model'] = isset($_POST['mc_type_model']) ? htmlspecialchars(trim($_POST['mc_type_model'])) : '';//规格型号
			$temp['plan_no'] = isset($_POST['plan_no']) ? intval($_POST['plan_no']) : 1;//申请数量
			$temp['make_time'] = isset($_POST['make_time']) ? intval($_POST['make_time']) : 0;//制造时间
			$temp['make_cost'] = isset($_POST['make_cost']) ? sprintf("%01.2f",$_POST['make_cost']) : 0;//造价
			$temp['manage_cost'] = isset($_POST['manage_cost']) ? sprintf("%01.2f",$_POST['manage_cost']) : 0;//管理费
			$temp['vat'] = isset($_POST['vat']) ? sprintf("%01.2f",$_POST['vat']) : 0;//增值税
			$temp['mc_info'] = isset($_POST['mc_info']) ? htmlspecialchars(trim($_POST['mc_info'])) : '';//备注
			$temp['unit'] = isset($_POST['unit']) ? htmlspecialchars(trim($_POST['unit'])) : '';//单位
			$temp['sell_type_id'] = isset($_POST['sell_type_id']) ? intval($_POST['sell_type_id']) : 1;//性质ID
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'虚拟设备主表管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'browse' => array('browse', '查看', 16, false),
					'imports'  => array('imports','导入',32,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
		/**
		 * 判断该虚拟设备ID是否存在批复关联表中
		 *
		 * @param unknown_type $v_mc_id
		 * @return 0 不存在 1存在
		 */
		function check_is_exist($v_mc_id)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			return $zy_plan_reply_mc_db->check_is_exist_by_mc_id($v_mc_id);
		}
}

?>